//
//  CommonViewGenerator.m
//  ICrEAM
//
//  Created by Cty on 2016/11/06.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CommonViewGenerator.h"
#import "PreviewBaseItemView.h"
@interface CommonViewGenerator()
@property (nonatomic,strong) NSArray *dataSource;
@property (nonatomic,strong) NSArray *childViewContainer;
@property (nonatomic,assign) BOOL isLast;
@end
#define viewWidth  ActualWidth(238)
#define viewHeight  ActualHeight(50)
#define colume1StartX ActualWidth(20)
#define colume2StartX viewWidth
#define colume3StartX 2*viewWidth
#define colume4StartX 3*viewWidth

#define line1StartY ActualHeight(15)
#define line2StartY viewHeight+line1StartY+ActualHeight(10);


@implementation CommonViewGenerator
+(instancetype)creatGeneratoredViewWithFrame:(CGRect)frame andDataSource:(NSArray *)src andIsLast:(BOOL)isLast{
    return [[CommonViewGenerator alloc]initWithFrame:frame andDataSource:src andIsLast:isLast];

}
-(instancetype)initWithFrame:(CGRect)frame andDataSource:(NSArray *)src andIsLast:(BOOL)isLast{
    self = [super initWithFrame:frame];
    if(self){
        self.dataSource = src;
        self.isLast = isLast;
        //保存数据源同时创建view布局
        [self setUpViews];
        
    }
    return self;
}
- (void)setUpViews{
    PreviewBaseItemView *refer;
    for(int i = 1;i<=self.dataSource.count;i++){
        int row = i/4+1;
        
        int colume = i%4;
        
        if(colume == 0){
            row--;
            colume = 4;
        }
        
        NSArray *ds = [[self.dataSource objectAtIndex:i-1] componentsSeparatedByString:@"*ICM*"];
        NSString *title = [ds objectAtIndex:0];
        NSString *subTitle = [ds objectAtIndex:1];
        
        PreviewBaseItemView *childView = [[PreviewBaseItemView alloc]initWithFrame:CGRectMake((CGFloat)[self getX:colume], (CGFloat)[self getY:row],(CGFloat)viewWidth, (CGFloat)viewHeight) andTitle:title andSubTitle:subTitle];
        refer = childView;
        [self addSubview:childView];
    }
    if(self.isLast == NO){
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, refer.bottomEdge+ActualHeight(15),ScreenWidth-2*ActualWidth(78), 1)];
        
        line.backgroundColor = Color(0xDDDDDD);
        
        [self addSubview:line];
    
    }
    
}

-(int)getX:(int)colume{
    int x = 0;
    switch (colume) {
        case 1:
            x= colume1StartX;
            break;
        case 2:
            x= colume2StartX;
            break;
        case 3:
            x= colume3StartX;
            break;
        case 4:
            x= colume4StartX;
            break;
        default:
            break;
    }
    return x;
}

-(int)getY:(int)row{
    int y;
    
    switch (row) {
        case 1:
            y = line1StartY;
            break;
        case 2:
            y = line2StartY;
            break;
        default:
            break;
    }
    
    return y;
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
